package com.scs.application.modules.base.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.exception.BusinessException;
import com.scs.application.core.utils.DateUtils;
import com.scs.application.core.utils.PinYinUtils;
import com.scs.application.modules.base.entity.*;
import com.scs.application.modules.base.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ Description：报表前置处理方法
 *  一般用作数据权限拼接
 */
@Slf4j
@Component
public class AfterImport {

    @Autowired
    UserWarehouseService userWarehouseService;
    @Autowired
    DeptService deptService;
    @Autowired
    MatrService matrService;
    @Autowired
    MatrUnitService matrUnitService;
    @Autowired
    MfgService mfgService;
    @Autowired
    MatrCategoryService matrCategoryService;
    @Autowired
    SupplierService supplierService;

    private static AfterImport services;

    @PostConstruct
    public void init() {
        services = this;
    }

    /**
     * @Description： 耗材导入后,耗材信息补全后处理方法
     */
    public String matrImport(){
        log.info("AfterImport.matrImport begin");
        Date begin = DateUtils.addMinutes(new Date(), -15);//查询15分钟前的
        List<Matr> unMatrs = matrService.list(Wrappers.<Matr>query().ge("gmt_create",DateUtils.format(begin, "yyyy-MM-dd HH:mm:ss")).isNull("name_py").isNull("supplier_id"));
        //先用该耗材的单位信息，生成对应的base_matr_unit耗材单位表的信息
        List<Matr> newMatrs = new ArrayList<Matr>();
        List<MatrUnit> matrUnits = new ArrayList<MatrUnit>();
        if(CollectionUtils.isEmpty(unMatrs)) throw new BusinessException("当前没有可导入的数据，或者本次所导入是临时表数据已经全部导入成功");
        for (Matr matr : unMatrs) {
            newMatrs.add(upDateMfgAndSp(matr));
            matrUnits.add(getMatrUnit(matr));
        }
        matrUnitService.saveBatch(matrUnits,100);
        matrService.saveOrUpdateBatch(newMatrs, 100);
        return "耗材导入后,耗材信息补全后处理";
    }

    //新增或者更新耗材基本单位
    private MatrUnit getMatrUnit (Matr matr){
        MatrUnit matrUnit = matrUnitService.getOne(new QueryWrapper<MatrUnit>().eq("matr_id", matr.getId()).ne("flag_base", true));
        if (null != matrUnit) {
            matrUnit.setPrice(matr.getSkuPrice()).setUnit(matr.getSkuUnit());
        } else {
            matrUnit = new MatrUnit();
            matrUnit.setMatrId(matr.getId()).setUnit(matr.getSkuUnit())
                    .setPrice(matr.getSkuPrice()).setRate(1.0)
                    .setFlagBase(true).setFlagPurchase(true).setFlagApply(true);
        }
        return matrUnit;
    }
    private Matr upDateMfgAndSp(Matr old){
        List<Mfg> mfgs = mfgService.list(new QueryWrapper<Mfg>().eq("name", old.getMfgName().trim()).orderBy(true,false,"gmt_create"));//"hrp_code", old.getHrpMfgCode()
        old.setMfgId(mfgs.size() > 0 ? mfgs.get(0).getId() : "0");//是0的都是未找到的
        List<MatrCategory> categorys = matrCategoryService.list(new QueryWrapper<MatrCategory>().eq("category_name", old.getCategoryName().trim()).orderBy(true,false,"gmt_create"));
        old.setCategoryId(categorys.size() > 0 ? categorys.get(0).getId() : "0");//是0的都是未找到的
        List<Supplier> suppliers = supplierService.list(new QueryWrapper<Supplier>().eq("name", old.getSupplierName().trim()).orderBy(true,false,"gmt_create"));
        old.setSupplierId(suppliers.size() > 0 ? suppliers.get(0).getId() : "0");//是0的都是未找到的
        old.setNamePy(PinYinUtils.getAlpha(old.getName()));
        return old;
    }
}
